<?php
namespace app\command;

use app\beautiful\model\order\BuyOrderModel;
use app\beautiful\model\order\PinTuanOrderModel;
use app\beautiful\model\setting\ConsumeBonusModel;
use app\beautiful\model\setting\ConsumeBonusSettingModel;
use app\common\model\DataRecordModel;
use app\common\model\MemberConsumeModel;
use app\stores\model\order\GoodsOrderModel;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\helper\Str;

class Memberconsume_buyorder extends Command
{
    protected function configure()
    {
        $this->setName('Memberconsume_buyorder')
            ->addArgument('name', Argument::OPTIONAL, "your name")
            ->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
            ->setDescription('Say Hello');
    }

    protected function execute(Input $input, Output $output)
    {
        $datarecord= new DataRecordModel();
        $record_id=2;
        $record=$datarecord->where(['id'=>$record_id])->find();
        $old_edittime=empty($record)?'':$record['old_edittime'];
        $old_edittime=strtotime($old_edittime);
        $page=1;
        $num=10;
        $order['a.edit_time']='asc';
        $order['a.buy_order_id']='asc';
        $h=date('H');
        $time=time();
        if($h=='00'){
            $time=time()-120;
        }
        //获取当天的开始和结束时间
        $day_start=strtotime(date('Y-m-d 00:00:00'));
        $day_end=strtotime(date('Y-m-d 23:59:59'));
        //本周的开始时间和结束时间
        $week_start = strtotime('this week',$time);
        $week_start=strtotime(date('Y-m-d 00:00:00',$week_start));
        $week_end = strtotime('next week',$week_start)-1;
        //获取本月的开始时间和结束时间
        $start_of_month = strtotime(date('Y-m-01 00:00:00',$time));
        $end_of_month = strtotime(date('Y-m-t 23:59:59',$time));
        //获取本季度的开始时间和结束时间
        $j_res=$this->jidu($time);
        $j_start=$j_res['startTime'];
        $j_end=$j_res['endTime'];
        //获取半年的开始和结束时间
        $b_res=$this->b_year($time);
        $b_start=$b_res['startTime'];
        $b_end=$b_res['endTime'];
        //获取当年的开始时间和结束时间
        $currentYear = date('Y',$time);
        $startTime = "{$currentYear}-01-01 00:00:00";
        $endTime = "{$currentYear}-12-31 23:59:59";
        $y_start=strtotime($startTime);
        $y_end=strtotime($endTime);
        do{
            $s=[];
            $h=date('H');
            $i=date('i');
            if($h=='00' && $i=='01'){
                $s['>=edit_time']=$old_edittime;
            }else{
                $s['edit_time']=$old_edittime;
            }
            $s['is_paid']=1;
            $list=BuyOrderModel::list($s,$order,$page,$num);
            if(empty($list)){
                $output->writeln("暂无数据".var_export([$s,$order,$page],true) );exit();
            }
            foreach ($list as $v){
                $u_s=[];
                $u_s['shop_id']=$v['shop_id'];
                $u_s['member_id']=$v['member_id'];
                $u_s['start_time']=$day_start;
                $u_s['end_time']=$day_end;
                $this->order_data($u_s,6);//更新天数据
                $this->sort_up($u_s,6);//更新排名
                $u_s['start_time']=$week_start;
                $u_s['end_time']=$week_end;
                $this->order_data($u_s,1);//更新周数据
                $this->sort_up($u_s,1);//更新排名
                $u_s['start_time']=$start_of_month;
                $u_s['end_time']=$end_of_month;
                $this->order_data($u_s,2);//更新月数据
                $this->sort_up($u_s,2);//更新排名
                $u_s['start_time']=$j_start;
                $u_s['end_time']=$j_end;
                $this->order_data($u_s,3);//更新季度数据
                $this->sort_up($u_s,3);//更新排名
                $u_s['start_time']=$b_start;
                $u_s['end_time']=$b_end;
                $this->order_data($u_s,4);//更新半年数据
                $this->sort_up($u_s,4);//更新排名
                $u_s['start_time']=$y_start;
                $u_s['end_time']=$y_end;
                $this->order_data($u_s,5);//更新年数据
                $this->sort_up($u_s,5);//更新排名
//                memberlock($v['buy_order_id'],2);//锁客分红
            }
            $end=end($list);
            if(strtotime($end['edit_time'])==$old_edittime){
                $page++;
            }else{
                $old_edittime=$end['edit_time'];
                $up=[];
                $up['old_edittime']=$old_edittime;
                DataRecordModel::up($up,['id'=>$record_id]);
            }
        }while(true);
    }

    public function jidu($time){
        $currentYear = date('Y',$time);
        $currentMonth = date('m',$time);
        if ($currentMonth >= 1 && $currentMonth <= 3) {
            $season = 1;
        } elseif ($currentMonth >= 4 && $currentMonth <= 6) {
            $season = 2;
        } elseif ($currentMonth >= 7 && $currentMonth <= 9) {
            $season = 3;
        } elseif ($currentMonth >= 10 && $currentMonth <= 12) {
            $season = 4;
        }

        switch ($season) {
            case 1:
                $startTime = "{$currentYear}-01-01 00:00:00";
                $endTime = "{$currentYear}-03-31 23:59:59";
                break;
            case 2:
                $startTime = "{$currentYear}-04-01 00:00:00";
                $endTime = "{$currentYear}-06-30 23:59:59";
                break;
            case 3:
                $startTime = "{$currentYear}-07-01 00:00:00";
                $endTime = "{$currentYear}-09-30 23:59:59";
                break;
            case 4:
                $startTime = "{$currentYear}-10-01 00:00:00";
                $endTime = "{$currentYear}-12-31 23:59:59";
                break;
        }
        $startTime=strtotime($startTime);
        $endTime=strtotime($endTime);
        return compact('startTime','endTime');
    }
    public function b_year($time){
        $currentYear = date('Y',$time);
        $currentMonth = date('m',$time);

        if ($currentMonth >= 1 && $currentMonth <= 6) {
            $half = 1;
        } elseif ($currentMonth >= 7 && $currentMonth <= 12) {
            $half = 2;
        }

        switch ($half) {
            case 1:
                $startTime = "{$currentYear}-01-01 00:00:00";
                $endTime = "{$currentYear}-06-30 23:59:59";
                break;
            case 2:
                $startTime = "{$currentYear}-07-01 00:00:00";
                $endTime = "{$currentYear}-12-31 23:59:59";
                break;
        }
        $startTime=strtotime($startTime);
        $endTime=strtotime($endTime);
        return compact('startTime','endTime');
    }
    public function order_data($where,$type){
        $where['status']=8;
        $uo_info=GoodsOrderModel::user_order($where);
        $where['status']=1;
        $ub_info=BuyOrderModel::user_order($where);
//        $pin_info=PinTuanOrderModel::user_order($where);
        $insert['member_id']=$where['member_id'];
        $insert['shop_id']=$where['shop_id'];
        $insert['start_time']=date('Y-m-d H:i:s',$where['start_time']);
        $insert['end_time']=date('Y-m-d H:i:s',$where['end_time']);
        $insert['type']=$type;
        $insert['total_money']=$uo_info['money']+$ub_info['money'];
        $insert['order_num']=$uo_info['all_num']+$ub_info['all_num'];
        $s=[];
        $s['member_id']=$where['member_id'];
        $s['shop_id']=$where['shop_id'];
        $s['start_time']=date('Y-m-d H:i:s',$where['start_time']);
        $s['type']=$type;
        $memberconsume=new MemberConsumeModel();
        $info=$memberconsume->where($s)->find();
        if(!empty($insert['total_money'])){
            if(empty($info)){
                $insert['create_time']=date('Y-m-d H:i:s');
                MemberConsumeModel::insert($insert);
            }else{
                MemberConsumeModel::up($insert,['id'=>$info['id']]);
            }
        }
    }
    public function sort_up($where,$type){
        unset($where['member_id']);
        unset($where['end_time']);
        $order['a.total_money']='desc';
        $order['a.member_id']='asc';
        $where['type']=$type;
        $where['start_time']=date('Y-m-d H:i:s',$where['start_time']);
        $list=MemberConsumeModel::list($where,$order);
        $consume=new ConsumeBonusSettingModel();
        $s=[];
        $s['shop_id']=$where['shop_id'];
        $shop_set=$consume->where($s)->find();
        $all_count=count($list);
        $ratio=$this->rank_ratio($shop_set,$type);
        $up=[];
        if($ratio===false){
            $up['status']=0;
        }else{
            $up['status']=1;
        }
        $partake=1;
        if(($all_count*$ratio)>1){
            $partake=floor($all_count*$ratio);
        }
        if(!empty($list)){
            foreach ($list as $k=>$v){
                $up['sort']=$k+1;
                $up['partake_status']=0;
                if($up['sort']<=$partake && $up['status']==1){
                    $up['partake_status']=1;
                }
                MemberConsumeModel::up($up,['id'=>$v['id']]);
            }
        }
    }
    public function rank_ratio($shop_set,$type){
        $now_type=[];
        $ratio=false;
        if($shop_set['is_open']==1){
            if($shop_set['bonus_cycle']==1){//单次分红
                if($shop_set['bonus_cycle_type']==1){//周
                    $now_type=[1];
                }elseif ($shop_set['bonus_cycle_type']==2){//月
                    $now_type=[2];
                }elseif ($shop_set['bonus_cycle_type']==3){//季度
                    $now_type=[3];
                }elseif ($shop_set['bonus_cycle_type']==6){//天
                    $now_type=[6];
                }
            }elseif ($shop_set['bonus_cycle']==2){//两次分红
                if($shop_set['bonus_cycle_type']==1){//周+月
                    $now_type=[1,2];
                }elseif ($shop_set['bonus_cycle_type']==2){//月+季度
                    $now_type=[2,3];
                }elseif ($shop_set['bonus_cycle_type']==3){//月+半年
                    $now_type=[2,4];
                }elseif ($shop_set['bonus_cycle_type']==4){//季度+半年
                    $now_type=[3,4];
                }elseif ($shop_set['bonus_cycle_type']==5){//季度+年
                    $now_type=[3,5];
                }
            }
        }
        if(in_array($type,$now_type)){
            if($type==1){//周
                $ratio=$shop_set['week_rank_ratio'];
            }elseif ($type==2){//月
                $ratio=$shop_set['month_rank_ratio'];
            }elseif ($type==3){//季度
                $ratio=$shop_set['quarter_rank_ratio'];
            }elseif ($type==4){//半年
                $ratio=$shop_set['half_year_rank_ratio'];
            }elseif ($type==5){//年
                $ratio=$shop_set['year_rank_ratio'];
            }elseif ($type==6){//天
                $ratio=$shop_set['day_rank_ratio'];
            }
            $ratio=round($ratio/100,2);
        }
        return $ratio;
    }
}